<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 捐赠日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">捐赠日期</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 支付爱心码 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/loveCode.png" />
          <span class="li_txt">支付爱心码</span>
        </div>
        <div class="label">
          <input type="number" :value="donateId" :disabled="true" />
        </div>
      </li>
      <!-- 捐赠金额(元) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/money.png" />
          <span class="li_txt">捐赠金额(元)</span>
        </div>
        <div class="label">
          <input type="number" v-model="donateAmount" />
        </div>
      </li>
      <!-- 付款方式 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/payment.png" />
          <span class="li_txt">付款方式</span>
        </div>
        <div class="label">
          <input type="text" v-model="donateType" placeholder="支付宝/微信/银行转账" />
        </div>
      </li>
      <!-- 受赠方 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/recipient.png" />
          <span class="li_txt">受赠方</span>
        </div>
        <div class="label">
          <input type="text" v-model="donatee" placeholder="请输入受赠方" />
        </div>
      </li>
      <!-- 确定按钮 -->
      <button class="btn" @click="btnDonate">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "Contribution",
  data() {
    return {
      userDate: "", // 展示给用户
      donateTime: "",
      show: false,
      donateId: "",
      disabled: false,
      donateAmount: "",
      donateType: "",
      donatee: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.donateTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");

    //随机生成爱心码
    let random = "";
    for (let a = 0; a < 20; a++) {
      random += Math.floor(Math.random() * 10);
    }
    this.donateId = random;
  },
  methods: {
    btnDonate() {
      this.loading = true; // loading显示
      let params = {
        token: JSON.parse(localStorage.getItem("login")).res.token,
        userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
        donateTime: this.donateTime,
        donateId: this.donateId,
        donateAmount: this.donateAmount,
        donateType: this.donateType,
        donatee: this.donatee
      };
      this.$api
        .donateCash(params)
        .then(res => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: res
          });
          this.$options.data();
        })
        .catch(err => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: err
          });
        });
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>
